<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<script type="text/javascript" src="<c:url value="/js/plug/plupload-2.1.2/zc-plupload.js" />"></script>
 

<c:set var="p" value="${param}"></c:set>
<div id="fileUploadTabs" class="easyui-tabs" data-options="tools:'#tab-tools',border:false"  style="width:100%; height: 100%;" >
	<div id="uploader" title="文件上传" closable="false"   >
		<div style="height: 1000px;"></div>
	</div>
	<div title="文件管理" style="width:100%;background: #e6edfd;height: 600px;"   closable="false">
		
		<div id="fileToolbar" style="display: none;">
	    	<table class="screen" style="height:30px;margin-left: 0px;background: none;">
				<tr>
					<td>
					   <a href="javascript:selectFiles();" class="easyui-linkbutton" data-options="iconCls:'ext-icon-plugin_add'">选择并返回</a>
    	   			   <a href="javascript:removeFile();" class="easyui-linkbutton" data-options="iconCls:'ext-icon-delete'">删除</a>
					</td>
					<th><span>文件名</span></th>
					<td class="screen-n">
						<input type="text"  class="screen-input" id="s_fileName" onkeydown="pms.kd13('initTable')">
					</td>
					<!-- 
					<th><span>目录</span></th>
					<td class="screen-n">
						<input type="text"  class="screen-input"  >
					</td>
					<th>年</th>
					<td class="screen-n">
						<select id="year"><option>2015</option><option>2014</option></select>
					</td>
					<th>月</th>
					<td class="screen-n">
						<select id="month">
						<option>12</option>
						<option>11</option>
						<option>10</option>
						<option>09</option>
						<option>08</option>
						<option>07</option>
						<option>06</option>
						<option>05</option>
						<option>04</option>
						<option>03</option>
						<option>02</option>
						<option>01</option>
						</select>
					</td>
					 -->
					<td>
						<a href="#" class="easyui-linkbutton" data-options="iconCls:'ext-icon-search'" onclick="initTable();return false;">查询</a>
					</td>
				</tr>
			</table>
		</div>
		<table id="fileUploadTable" style="width:100%;height:400px;" > </table>
		 
	</div>
	
<!-- 	<div title="文件磁盘管理" style="width:100%;"   closable="false">
		建设中......
	</div> -->
</div>
<script type="text/javascript">
var tb;
var params = {
		"moduleId": '<c:out value="${p.moduleId}" />',
		"funcId" : '<c:out value="${p.funcId}" />',
 		"dataId" : '<c:out value="${p.dataId}" />', 
		"fileType" : '<c:out value="${p.fileType}" />',
		"oneSelect":'<c:out value="${p.oneSelect}" />',
		"isPicZoom":'<c:out value="${p.isPicZoom}" />'
	};
function checkParams(){
	if(params.moduleId==""){
		parent.alertError("缺少模块参数 moduleId!");
		parent.$("#plug_upload").dialog('close');
	}
	if(params.funcId==""){
		parent.alertError("缺少功能参数 funcId!");
		parent.$("#plug_upload").dialog('close');
	}
	if(params.fileType==""){
		parent.alertError("缺少文件类型fileType 请指定文件file 或者图片img!");
		parent.$("#plug_upload").dialog('close');
	}
	if(params.oneSelect == ""){
		parent.alertError("必须指定是否单选 oneSelect!");
		parent.$("#plug_upload").dialog('close');
	}else{
		if(params.oneSelect=="true"){
			params["singleSelect"]=true;
		}else{
			params["singleSelect"]=false;
		}
	}
}
checkParams();

/*******使用插件 为下面2段********/
$("#uploader").zcinitupload({ //初始化
	path:"<c:url value='/js/plug/plupload-2.1.2' />"
});
function zcup(){
	//console.log(params.fileType);
	$("#uploader").zcplupload({   //上传绑定
		url : '<c:url value="/upload/uploadFiles.do"/>',
		plugPath:'/js/plug/plupload-2.1.2',
		fileType:params.fileType,
		params:params,
		FilesAdded: function(up, files) {
			plupload.each(files, function(file) {
				alert(1);
				//document.getElementById('filelist').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>';
			});
		}
	});
}
/*******end********/

$(function() {
	zcup();   //绑定上传组件
	$('#fileUploadTabs').tabs({
		onSelect : function(title, index) {
			if (index == 0) {
				zcup();
			}
			if (index == 1) {
				tabChange(title, index);
			}
		}
	});

	//绑定图片预览
	 
});
//加载 上传列表
function initTable(){
	
	tb = $('#fileUploadTable').pDataGrid({
		toolbar:'#fileToolbar',
	    columns:[[
	              {field:'ck',checkbox:true},
	              {field:'uploadId',title:'ID',align:'center'},
	              {field:'viewsmall',title:'图标',align:'center',formatter:function(value,rows){
		                if(rows.fileType =="img"){
							return '<a href="'+contentPath+rows.relativeFilePath+"/"+rows.fileSysName+'" target="_blank" ><span style="padding:1px;"><img src="'+contentPath+rows.relativeFilePath+"/small_"+rows.fileSysName+'" width="30px;" height="30px;"  /></span></a>';
		                }else if(rows.fileType =="file"){
		                	return '<span style="padding:1px;"><img src="'+contentPath+rows.fileIcons+'" width="30px;" height="30px;" /></span>';
			            }else if(rows.fileType =="all"){
							//未存类型
			            	return '<span style="padding:1px;"><img src="'+contentPath+rows.fileIcons+'" width="30px;" height="30px;" /></span>';
				        }
				  }},
	              {field:'fileName',title:'文件名',align:'center'},
	             
	              {field:'fileExtensions',title:'后缀',align:'center'},
	              {field:'fileSizeKb',title:'大小',align:'center'},
	              {field:'creationDate',title:'上传时间',align:'center'},
	              {field:'relativeFilePath',title:'目录',align:'center'},
	              {field:'fileSysName',title:'存储名',align:'center'}
	              
	          ]],
	    url:'<c:url value="/upload/searchPageEmBUpload.do"/>',  
	    pagination:true,//分页控件
	    pageSize:10,
	    queryParams:$.extend(params,{"fileName":$("#s_fileName").val()}),
	    singleSelect:params.singleSelect
	}); 

	
}
function tabChange(title,index){
	if(tb==null){
		initTable();
	}else{
		tb.refresh();
	}
}

//选择勾选的文件
function selectFiles(){
	var row = $('#fileUploadTable').datagrid('getSelections');
	if(row.length==0){
		alertWarning("您未选择任何文件!");
		return;
	}
	selectFileArr = [];
	for ( var i = 0; i < row.length; i++) {
		var selectFileObj = {};
		selectFileObj['id']= row[i].uploadId;
		
		selectFileObj['fileWebPath'] = row[i].relativeFilePath+row[i].fileSysName;
		
		selectFileObj['fileName'] = row[i].fileName;
		selectFileObj['fileSysName'] = row[i].fileSysName;
		selectFileObj['fileIcons'] = row[i].fileIcons;
		selectFileObj['fileExtensions'] = row[i].fileExtensions;
		selectFileObj['relativeFilePath'] = row[i].relativeFilePath;
		selectFileObj['funcId'] = row[i].funcId;
		
		selectFileArr.push(selectFileObj);
	}
	parent.getFiles(selectFileArr);
	parent.$("#plug_upload").dialog('close');
}
/**
*<p>删除文件逻辑</p>
*/
function removeFile() {
	var row = $('#fileUploadTable').datagrid('getSelections');
	if (row) {
		$.messager.confirm('确认', '您确定需要删除该条记录吗？', function(r) {
			var ids = '';
			for ( var i = 0; i < row.length; i++) {
				if (i == row.length - 1) {
					ids += row[i].uploadId;
				} else {
					ids += row[i].uploadId + ',';
				}
			}
			if (r) {
				$.ajax({
					type : "POST",
					url : "<c:url value='/upload/deleteMultipleEmBUpload.do'/>",
					data : 'uploadIdList=' + ids.toString(),
					success : function(data) {
						if (data.success) {
							alertShow("删除成功");
							tb.refresh();
						} else {
							alertError("删除失败");
						}
					}
				});
			}
		});
	} else {
		alertWarning('请选择一条数据！');
	}
}
</script>
